<template>
<div>
   <van-nav-bar title="黑马头条-登录" />
   <van-form @submit="onSubmit">
  <van-field
    v-model="formLogin.mobile"
    name="手机号"
    label="手机号"
    placeholder="请输入手机号"
    required
    :rules="[{ required: true, message: '请填写11位数字' ,pattern:/^1[3-9]\d{9}$/,trigger:'onChange' }]"
  />
  <van-field
    v-model="formLogin.code"
    type="password"
    name="密码"
    label="密码"
    placeholder="密码"
    required
    :rules="[{ required: true, message: '请填写六位密码' , trigger: 'onChange',pattern: /^\d{6}$/ }]"
  />
  <div style="margin: 16px;">
    <van-button round block type="info" native-type="submit" :loading="isLoding" :disabled="isLoding">登录</van-button>
  </div>
</van-form>
    </div>
</template>

<script>
import { mapActions } from 'vuex'
import { Notify } from 'vant'
export default {
  name: 'Login',
  data () {
    return {
      isLoding: false,
      formLogin: {
        mobile: '13888888888',
        code: '246810'
      }
    }
  },
  methods: {
    ...mapActions(['asyncLoginAction']),
    async onSubmit (values) {
      this.isLoding = true
      try {
        await this.asyncLoginAction(this.formLogin)
        Notify({ type: 'success', message: '登录成功' })
        this.isLoding = false
        this.$router.replace('/layout')
        // router跳转有两个方法
        // this.$router.replace()   不可以回退
        // this.$router.push()    可以回退
      } catch (err) {
        Notify({ type: 'warning', message: '手机号或密码错误' })
        this.isLoding = false
      }
    }
  }

}
</script>

<style lang="less" scoped>

// .van-nav-bar{
//   background-color: #007bff;
// }
// 因为组件上用了scoped在打包过程中会给dom添加个类用做样式的隔离
// 可以在类名前加个 /deep/实现穿透来改变子组件的样式
// /deep/.van-nav-bar__title{
//   color: #fff;
// }
// 当前的预处理器就是lang ="less"
// 预处理器不同穿透符也不同     scss::deep     stylus >>>
</style>
